package com.sgb.merchant.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.merchant.DynamicListEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantActivityEditDynamicBinding;
import com.sgb.merchant.viewmodel.IndustryViewModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;

import java.util.Date;

/**
 * @author: 王彬臣
 * @date: 2021/10/14
 * @Description: 编辑动态详情页
 */
@Route(path = PathMerchant.EditDynamicActivity)
public class EditDynamicActivity extends BaseActivity<IndustryViewModel, MerchantActivityEditDynamicBinding> {

    @Autowired(name = RouterKey.NO)
    String no;
    @Autowired(name = RouterKey.BOOLEAN)
    boolean isEdit;
    @Autowired(name = RouterKey.DATA)
    DynamicListEntity dynamicListEntity;

    @Override
    protected int layoutId() {
        return R.layout.merchant_activity_edit_dynamic;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(IndustryViewModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.setOnClick(this);
        if (dynamicListEntity==null) {
            dynamicListEntity = new DynamicListEntity();
        }
        mBinding.itvName.setContent(dynamicListEntity.getTrendTitle());
        mBinding.etDesc.setEditContent(dynamicListEntity.getTrendInfo());


        mBinding.actionBar.setOnBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_confirm) {
            if (!ClickUtils.isDoubleClick()) {
                String title = mBinding.itvName.getContent();
                String info = mBinding.etDesc.getEditContent();
                if (TextUtils.isEmpty(title)) {
                    MToast.showToast(this, "请输入动态标题");
                    return;
                }
                if (TextUtils.isEmpty(info)) {
                    MToast.showToast(this, "请输入动态简介");
                    return;
                }
                dynamicListEntity.setTrendInfo(info);
                dynamicListEntity.setTrendTitle(title);
                dynamicListEntity.setCreateTime(new Date().getTime());

                if (TextUtils.isEmpty(no)) {
                    Intent intent = new Intent();
                    intent.putExtra("info", dynamicListEntity);
                    intent.putExtra(RouterKey.BOOLEAN, isEdit);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    dynamicListEntity.setIndustrialParkNo(no);
                    mViewModel.editDemands(this, dynamicListEntity);
                }


            }
        }
    }
}
